/*
 * @Date: 2024-01-04 13:54:46
 * @LastEditors: lyx
 * @LastEditTime: 2024-01-04 13:54:54
 * @FilePath: \mt-middleware-server\utils\common\getReqMainInfo.ts
 * @Description: 获取请求头信息的工具方法
 */
import { Request } from 'express';

export const getReqMainInfo: (req: Request) => {
  [prop: string]: any;
} = (req) => {
  const { query, headers, url, method, body, connection } = req;

  // 获取 IP
  const xRealIp = headers['X-Real-IP'];
  const xForwardedFor = headers['X-Forwarded-For'];
  const { ip: cIp } = req;
  const { remoteAddress } = connection || {};
  const ip = xRealIp || xForwardedFor || cIp || remoteAddress;

  return {
    url,
    host: headers.host,
    ip,
    method,
    query,
    body,
  };
};
